{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation and deployment of 5G RAN (gNB) of Open Air Interface (OAI)\n",
    "## Objectives\n",
    "- Install and configure the OAI gNB \n",
    "- Deploy a gNB together a previous configured 5GC\n",
    "- Inspec gNB status in 5GC Core"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At the end of a set of three hands-on (this is the second one), the goal is to run the simulated OAI scenario, which consists of the 5G core (5GC), gNB in CU/DU split mode, and user equipment (UE), according to the image below: \n",
    "\n",
    "![5GC_01](../FIGS/H01_5GCore_UNI_III/5GC_deploy.jpg)\n",
    "\n",
    "Now, we are going to install, configure, deploy and interact to gNB with CU/DU split mode. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gNB Installation\n",
    "\n",
    "To install OAI RAN 5G, go back to /home/dco1020 and follow this tutorial.\n",
    "\n",
    "**Step 0:** go back to your home folder:\n",
    "\n",
    "```\n",
    "cd ~ \n",
    "```\n",
    "\n",
    "\n",
    "**Step 1:** Install pre-requisites\n",
    "\n",
    "Open a terminal in your home directory, then execute the following commands:\n",
    "\n",
    "```\n",
    "sudo apt install -y autoconf automake build-essential ccache cmake cpufrequtils doxygen ethtool g++ git inetutils-tools libboost-all-dev libncurses5 libncurses5-dev libusb-1.0-0 libusb-1.0-0-dev libusb-dev python3-dev python3-mako python3-numpy python3-requests python3-scipy python3-setuptools python3-ruamel.yaml\n",
    "```\n",
    "\n",
    "```\n",
    "sudo apt-get install libuhd-dev uhd-host\n",
    "```\n",
    "\n",
    "**Step 2:** Download OAIgNB and nrUE files\n",
    "\n",
    "Open a terminal in OAI directory, then execute the following command lines:\n",
    "```\n",
    "git clone https://gitlab.eurecom.fr/oai/openairinterface5g.git\n",
    "```\n",
    "```\n",
    "cd openairinterface5g\n",
    "```\n",
    "```\n",
    "git checkout v2.1.0\n",
    "```\n",
    "\n",
    "**Step 3:** Build the configuration files\n",
    "\n",
    "Open a terminal in the openairinterface5g repository:\n",
    "```\n",
    "cd cmake_targets\n",
    "```\n",
    "```\n",
    "./build_oai -I\n",
    "```\n",
    "```\n",
    "sudo apt install -y libforms-dev libforms-bin\n",
    "```\n",
    "```\n",
    "./build_oai -w USRP --ninja --nrUE --gNB --build-lib \"nrscope\" -C\n",
    "```\n",
    "\n",
    "**Attention:** Be patient, the last command could take a whileeeee. If you want to follow the installation logs, run the command below:\n",
    "```\n",
    "tail -f /home/YOUR_USER/OAI/openairinterface5g/cmake_targets/log/all.txt\n",
    "```\n",
    "\n",
    "**Attention**: you may find Wireshark configuration screen, select \"yes\" to continue.\n",
    "\n",
    "\n",
    "If you have any compilation error, please, try:\n",
    "\n",
    "Take care about conflits between our VM and some windows configuration, If you have compilation problem, please, check and disable WSL, hipervison and virtual machine platforms, acoording to the following figures.\n",
    "\n",
    "\n",
    "![gNB_problem1](../FIGS/H02_5GRAN_UNI_III/gNB_problem1.png)\n",
    "\n",
    "![gNB_problem2](../FIGS/H02_5GRAN_UNI_III/gNB_problem2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gNB Configuration for the split mode (CU/DU)\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 1:** Configure the PLMN and TAC values\n",
    "\n",
    "The Public Land Mobile Network (PLMN) and Tracking Area Code (TAC) will be configured in the CU (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf) and DU (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf) configuration files. \n",
    "\n",
    "Figure below shows the part of the code that must be modified in the CU and DU files (lines 17 and 18). The parameters shown in figure should be modified according to the values present in docker compose in the \"oai-amf\" session (/home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml). \n",
    "\n",
    "![gNB_config_01](../FIGS/H02_5GRAN_UNI_III/gNB_config_01.png)\n",
    "\n",
    "\n",
    "Figure below shows the parameters that should be checked and passed to the CU and DU configuration files: PLMN_SUPPORT_TAC (referring to \"tracking_area_code\"), PLMN_SUPPORT_MCC (referring to \"mcc\"), PLMN_SUPPORT_MNC (referring to \"mnc\"), SST_0 (referring to \"sst\") and the SD_0 (referring to \"sd\"). If the \"sd\" parameter is not in the CU and DU configuration files, simply add it according to line 18 in previous figure.\n",
    "\n",
    "![gNB_config_02](../FIGS/H02_5GRAN_UNI_III/gNB_config_02.png)\n",
    "\n",
    "\n",
    "Open files by typing:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf\n",
    "\n",
    "gedit /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml\n",
    "\n",
    "**Step 2:**  Configure the network interfaces\n",
    "\n",
    "This part you will configure the N2 interface in (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf), such as in figure below. This interface is responsible to connect the CU and the AMF.\n",
    "\n",
    "![gNB_config_03](../FIGS/H02_5GRAN_UNI_III/gNB_config_03.png)\n",
    "\n",
    "You need to verify/change the IP addresses. You can find the OAI-AMF IP address inside /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml, such as in figure below. You must get this IP address and put it in the \"amf_ip_address\" parameter, in the \"ipv4\" field (such as in previous figure).\n",
    "\n",
    "![gNB_config_04](../FIGS/H02_5GRAN_UNI_III/gNB_config_04.png)\n",
    "\n",
    "\n",
    "Check if the IP Address of cu_gnb.conf is the same of docker-compose-basic-nrf.yaml. To edit cu_gnb.conf, type:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf\n",
    "\n",
    "\n",
    "**Step 3:** to check a virtual interface (demo-oai), type:\n",
    "\n",
    "ifconfig -a\n",
    "\n",
    "You should find a virtual interface (demo-oai) (as shown in figure below). This interface serves to connect the CU with OAI-AMF.  You must get its IP address and name to configure the N2 interface such as two figures before. \n",
    "\n",
    "![gNB_config_05](../FIGS/H02_5GRAN_UNI_III/gNB_config_05.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gNB Configuration for the monolithic mode\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 1:** Configure the PLMN and TAC values\n",
    "\n",
    "The Public Land Mobile Network (PLMN) and Tracking Area Code (TAC) will be configured in the monolithic RAN (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf) configuration file. \n",
    "\n",
    "Figure below shows the part of the code that must be modified in the monolithic file (lines 17 and 18). The parameters shown in figure should be modified according to the values present in docker compose in the \"oai-amf\" session (/home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml). \n",
    "\n",
    "![gNB_config_01](../FIGS/H02_5GRAN_UNI_III/gNB_config_01.png)\n",
    "\n",
    "\n",
    "Figure below shows the parameters that should be checked and passed to the monolithic configuration file: PLMN_SUPPORT_TAC (referring to \"tracking_area_code\"), PLMN_SUPPORT_MCC (referring to \"mcc\"), PLMN_SUPPORT_MNC (referring to \"mnc\"), SST_0 (referring to \"sst\") and the SD_0 (referring to \"sd\"). If the \"sd\" parameter is not in the monolithic configuration file, simply add it according to line 18 in previous figure.\n",
    "\n",
    "![gNB_config_02](../FIGS/H02_5GRAN_UNI_III/gNB_config_02.png)\n",
    "\n",
    "\n",
    "Open files by typing:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf\n",
    "\n",
    "gedit /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml\n",
    "\n",
    "**Step 2:**  Configure the network interfaces\n",
    "\n",
    "This part you will configure the N2 interface in (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf), such as in figure below. This interface is responsible to connect the RAN and the AMF.\n",
    "\n",
    "![gNB_config_03](../FIGS/H02_5GRAN_UNI_III/gNB_config_03.png)\n",
    "\n",
    "You need to verify/change the IP addresses. You can find the OAI-AMF IP address inside /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml, such as in figure below. You must get this IP address and put it in the \"amf_ip_address\" parameter, in the \"ipv4\" field (such as in previous figure).\n",
    "\n",
    "![gNB_config_04](../FIGS/H02_5GRAN_UNI_III/gNB_config_04.png)\n",
    "\n",
    "\n",
    "Check if the IP Address of gnb.sa.band78.fr1.106PRB.usrpb210.conf is the same of docker-compose-basic-nrf.yaml. To edit gnb.sa.band78.fr1.106PRB.usrpb210.conf, type:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf\n",
    "\n",
    "\n",
    "**Step 3:** to check a virtual interface (demo-oai), type:\n",
    "\n",
    "ifconfig -a\n",
    "\n",
    "You should find a virtual interface (demo-oai) (as shown in figure below). This interface serves to connect the gNodeB with OAI-AMF.  You must get its IP address and name to configure the N2 interface such as two figures before. \n",
    "\n",
    "![gNB_config_05](../FIGS/H02_5GRAN_UNI_III/gNB_config_05.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deployment for the splited RAN \n",
    "\n",
    "The deployment of the CU and DU is done separately, and the CU needs to be started before the DU. \n",
    "\n",
    "**NOTE:** The CU and DU will be running on two different terminals\n",
    "\n",
    "**Step 1:** open a new terminal using crtl+Alt+T and type:\n",
    "\n",
    "sudo su\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf --continuous-tx \n",
    "\n",
    "**Step 2:** Open another terminal using crtl+Alt+T and type: \n",
    "\n",
    "sudo su \n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf --continuous-tx --gNBs.[0].min_rxtxtime 6\n",
    "\n",
    "\n",
    "**Step 3**: To check the connection of gNodeB to the Core, see AMF logs. Open another terminal (the third one) using crtl+Alt+T and type: \n",
    "\n",
    "docker logs -f oai-amf\n",
    "\n",
    "The expected return is shown in figure below, where gNodeB shows the connected status.\n",
    "\n",
    "![gNB_config_06](../FIGS/H02_5GRAN_UNI_III/gNB_config_06.png)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deployment for the monolithic RAN \n",
    "\n",
    "The deployment of the monolithic RAN  without dashboard, but if you want to deploy the OAI gNB dashboard follow this tutorial: [OAI gNB dashboard](https://github.com/vicentesousa/PPGEEC_2333/blob/main/howtos/H02_5G_OAI_dashboard.ipynb).\n",
    "\n",
    "**Step 1:** open a new terminal using ctrl+Alt+T and type:\n",
    "\n",
    "sudo su\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf --gNBs.[0].min_rxtxtime 6 --continuous-tx \n",
    "\n",
    "\n",
    "**Step 2**: To check the connection of gNodeB to the Core, see AMF logs. Open another terminal (the third one) using ctrl+Alt+T and type: \n",
    "\n",
    "docker logs -f oai-amf\n",
    "\n",
    "The expected return is shown in figure below, where gNodeB shows the connected status.\n",
    "\n",
    "![gNB_config_06](../FIGS/H02_5GRAN_UNI_III/gNB_config_06.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deliverable 02:\n",
    "\n",
    "- Inspec the gNB configuration files and change its name to gNB-DCO1020. Show the new gNB name in the AMF log file."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
